package yja.gzw.app.fragmentview

import android.content.Intent
import android.widget.Toast
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
import com.scwang.smart.refresh.layout.listener.OnRefreshListener
import yja.gzw.app.R
import yja.gzw.app.adapter.TuijianAdapter
import yja.gzw.app.adapter.ZhuanquAdapter
import yja.gzw.app.base.BaseFragment
import yja.gzw.app.databinding.FragmentTuijianBinding
import yja.gzw.app.model.UserData
import yja.gzw.app.model.bean.QueryItem
import yja.gzw.app.viewmodel.Mainviewmodel

class TuijianFragment :BaseFragment<Mainviewmodel, FragmentTuijianBinding>(R.layout.fragment_tuijian), TuijianAdapter.OnItemClickListener{
    private lateinit var listadapter2: TuijianAdapter
    private var shuju= mutableListOf<String>("1","2","3","4","1","2","3")
    var xiaobiao=0
    var neinei=""
    var yeshu=1
    var shujus= mutableListOf<QueryItem>()

    override fun initView() {
        viewmodel.ontuijianwang(UserData.token,mapOf("page" to "1"))

        binding.refreshLayout.setRefreshHeader(ClassicsHeader(context));
        binding.refreshLayout.setRefreshFooter(ClassicsFooter(context));
        binding.refreshLayout.setOnRefreshListener(object : OnRefreshListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                shuju.clear()
                yeshu=1
                viewmodel.ontuijianwang(UserData.token,mapOf("page" to "1"))

            }


        })

        binding.refreshLayout.setOnLoadMoreListener(object : OnLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {

                if(yeshu==((shuju.size/10)+1)){

                }else{
                    yeshu++
                }

                viewmodel.ontuijianwang(UserData.token,mapOf("page" to yeshu.toString()))

            }


        })




    }

    override fun dataObservese() {


            viewmodel.ondianzan.observe(this, Observer{
                    if(it.code==1){

                        viewmodel.ontuijianwang(UserData.token,mapOf("page" to "1"))


                    }else{
                        Toast.makeText(context,it.msg, Toast.LENGTH_SHORT).show()


                    }

                })

        viewmodel.ontuijian.observe(this, Observer {
            if(it.code==1){

                if(it.data.data.size<10&&yeshu==1){

                    binding.liebiao.layoutManager=
                        LinearLayoutManager(context)
                    listadapter2= TuijianAdapter(requireContext(),it.data.data,this)
                    binding.liebiao.adapter= listadapter2
                    binding.refreshLayout.finishRefresh();
                    binding.refreshLayout.finishLoadMore()

                }else{
                    shujus.addAll(it.data.data)
                    binding.liebiao.layoutManager=
                        LinearLayoutManager(context)
                    listadapter2= TuijianAdapter(requireContext(),shujus,this)
                    binding.liebiao.adapter= listadapter2
                    binding.refreshLayout.finishRefresh();
                    binding.refreshLayout.finishLoadMore()
                    if(yeshu==1){
                        binding.liebiao.scrollToPosition(0)

                    }else{
                        val shshbu=10*(yeshu-1)
                        binding.liebiao.scrollToPosition(shshbu-2)

                    }
                }




            }else{

                Toast.makeText(context,it.msg, Toast.LENGTH_SHORT).show()
            }


        })



//        viewmodel.ontuijian.observe(this, Observer{
//            if(it.code==1){
//                Toast.makeText(context,it.msg, Toast.LENGTH_SHORT).show()
//                binding.liebiao.layoutManager=
//                    LinearLayoutManager(context, LinearLayoutManager.VERTICAL,false)
//                listadapter2= TuijianAdapter(requireContext(),it.data.data,this)
//                binding.liebiao.adapter= listadapter2
//                binding.liebiao.scrollToPosition(0)
//
//            }else{
//                Toast.makeText(context,it.msg, Toast.LENGTH_SHORT).show()
//            }
//
//
//        })



    }

    override fun onItemClick(position: Int, uri: QueryItem) {

        viewmodel.ondianzanwang(UserData.token,mapOf("forum_id" to uri.id.toString()))

    }

}